package org.wk.core.common.view;

import java.util.HashMap;
import java.util.LinkedHashMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 
 * @ClassName : ResponseVo
 * @Package ：org.wk.core.common.view
 * @Author df.w
 * @Create Time ：2018年5月3日 下午3:43:14
 * @Description : 数据相应对象
 */
public class ResponseVo extends LinkedHashMap<String, Object> {
	/**
	 * @Fields serialVersionUID: long
	 * @Desc TODO:
	 */
	private static final long serialVersionUID = 1L;


	public ResponseVo() {
	}

	public <T> ResponseVo(PageVo<T> pageVo) {
		this.put("pageNo", pageVo.getPageNo());
		this.put("pageSize", pageVo.getPageSize());
		this.put("result", pageVo.getResult());
		this.put("total", pageVo.getTotal());
	}

	public ResponseVo(GeneralResponseCode responseCode) {
		put("code", responseCode.getCode());
		put("message", responseCode.getMessage());
		put("data", new HashMap<>());
	}

	public static ResponseVo success() {
		return success("操作成功");
	}
	public static ResponseVo success(String msg) {
		return success("success",msg);
	}

	public static ResponseVo success(String code, String msg) {
		ResponseVo r = new ResponseVo();
		r.put("message", msg);
		r.put("code", code);
		r.setData(new HashMap<>());
		return r;
	}

	public static ResponseVo error() {
		return error("操作失败");
	}
	public static ResponseVo error(String msg) {
		return error("error",msg);
	}
	public static ResponseVo error(String code, String msg) {
		ResponseVo r = new ResponseVo();
		r.put("message", msg);
		r.put("code", code);
		r.setData(new HashMap<>());
		return r;
	}


	public ResponseVo setCode(String code) {
		put("code", code);
		return this;
	}
	
	public ResponseVo setMessage(String message) {
		put("message", message);
		return this;
	}
	
	public ResponseVo setData(Object data) {
		put("data", data);
		return this;
	}

	public ResponseVo addAttribute(String attributeName, Object attributeValue) {
		super.put(attributeName, attributeValue);
		return this;
	}
	
	/*
	 * (非 Javadoc) <p>Title: toString</p> <p>Description: </p>
	 * 
	 * @return
	 * 
	 * @see java.lang.Object#toString()
	 */

	@Override
	public String toString() {
		return JSON.toJSONString(this,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty,
				SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullBooleanAsFalse);
	}

}
